package Extra;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class SwingLogin {
	public static final  Dimension FRAME_SIZE = new Dimension(1000, 130); 
	JFrame login = new JFrame("User Login");
	JLabel userlbl = new JLabel("Username ");
	JTextField usertxt =  new JTextField(20);
	JLabel passlbl = new JLabel("Password ");
	JPasswordField passtxt =   new JPasswordField(20);
	JButton loginbtn = new JButton("Login");
	JButton clearbtn = new JButton("Clear");
	JLabel txtlbl = new JLabel("");
	
	public void display() {
			 Container contentPane = login.getContentPane();
			 contentPane.setLayout(new FlowLayout());
			 contentPane.setPreferredSize(FRAME_SIZE);
			login.setSize(FRAME_SIZE);
			login.setBackground(Color.LIGHT_GRAY);
			contentPane.add(userlbl);
			contentPane.add(usertxt);
			contentPane.add(passlbl);
			contentPane.add(passtxt);
			contentPane.add(loginbtn);
			contentPane.add(clearbtn);
			contentPane.add(txtlbl);
			login.setVisible(true);
			loginbtn.addActionListener(new java.awt.event.ActionListener() {
	        public  void actionPerformed(java.awt.event.ActionEvent evt) {
	                try {
						loginbtnActionPerformed(evt);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }
	        });
			
			clearbtn.addActionListener(new java.awt.event.ActionListener() {
		        public  void actionPerformed(java.awt.event.ActionEvent evt) {
		                clearbtnActionPerformed(evt);
		            }
		        });
		}
				
			
		
		
	//method for login button
	@SuppressWarnings("deprecation")
	public  void loginbtnActionPerformed(java.awt.event.ActionEvent evt) throws Exception{
		//txtlbl.setText("Username is " + usertxt.getText() + ", Password is " + passtxt.getText());
		//System.out.println(txtlbl.getText());
		
		Class.forName("org.sqlite.JDBC");
	    Connection conn =
	      DriverManager.getConnection("jdbc:sqlite:/Users/hbargi/MySQLiteDB");
	    Statement stat = conn.createStatement();
	  
	    ResultSet rs = stat.executeQuery("select from users;");
	    while (rs.next()) {
	      if(rs.getString("username").equals(usertxt.getText()) && rs.getString("password").equals(passtxt.getText()))
	      {
	    	  txtlbl.setText("Login successful!");
	      }
	      else
	      {
	    	  txtlbl.setText("Username & password doesn't match; Please try again!");
	      }
	      //System.out.println("pass = " + rs.getString("password"));
	    }
	    rs.close();
	    conn.close();
	  }



	//method for clear button
	private  void clearbtnActionPerformed(java.awt.event.ActionEvent evt) {
		System.exit(-1);	
	}


public static void main(String args[])
	{
		SwingLogin sw = new SwingLogin();
		sw.display();
	}
}
